import redis


class RedisTool(object):
    """
    redis数据库工具
    """

    def __init__(self, product, env):
        """
        选择需要访问的数据库
        :param product:
        :param env: 选择指定的环境
        """
        self.direct = {}
        test = {
            'host': '',
            'port': '',
            'password': '',
            'db': ''
        }

        if product is '' and env is '':
            self.direct = test
        elif product == '' and env == '':
            self.direct = test

        self.pool = redis.ConnectionPool(
            host=self.direct.get('host'),
            port=self.direct.get('port'),
            password=self.direct.get('password'),
            db=self.direct.get('db')
        )
        self.r = redis.Redis(connection_pool=self.pool)

    def get_instance(self):
        return self.r

    def keys(self, str):
        if str == '' or str is None:
            return '参数不能为空'
        val = '*' + str + '*'
        a = self.r.keys(pattern=val)
        if a is None:
            print('no data')
            return self
        for i in a:
            print(i)
        return self

    def get(self, str):
        print(self.r.get(str))
        return self

    def set(self, key, value):
        self.r.set(key, value)
        return self

    def hgetall(self, str):
        print(self.r.hgetall(str))
        return self

    def hget(self, key, valu):
        print(self.r.hget(key, valu))
        return self

    def hset(self, key, valu):
        print(self.r.hset(key, valu))
        return self

    def delete(self, name):
        print(self.r.delete(name))
        return self


if __name__ == '__main__':
    RedisTool('', '').keys('').get(b'sms_code:2:login:')
    RedisTool('', '').keys('').hget(b'user_auth:', 0)
